﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Video;

namespace Mga
{
    public class CUIPVModule : MonoBehaviour
    {

        public RawImage Img_Screen;
        public GameObject Obj_VideoPlayer;
        public GameObject Obj_BtPass;
        public GameObject Obj_BtPause;
        public GameObject Obj_BtPlay;

        private GameObject m_Buttons;
        private VideoPlayer m_VideoPlayer;
        private AudioSource m_AudioSource;
        private VideoClip videoToPlay;
        private bool m_bModuleReady = false;
        private int LastBgmId;

        private float FixedWidth = 1920f;
        private float FixedHeight = 1080f;

        void Start()
        {
            if (Obj_VideoPlayer != null)
            {
                m_VideoPlayer = Obj_VideoPlayer.GetComponent<VideoPlayer>();
                m_AudioSource = Obj_VideoPlayer.GetComponent<AudioSource>();
            }
            //if (Img_Screen != null) CUIEventTriggerListener.Get(Img_Screen.gameObject).onClick = _onclickVideo;
            if (Obj_BtPass != null)
            {
                //CUIEventTriggerListener.Get(Obj_BtPass).onClick = _onclickPass;
                m_Buttons = Obj_BtPass.transform.parent.gameObject;
            }
            //if (Obj_BtPause != null) CUIEventTriggerListener.Get(Obj_BtPause).onClick = _onclickPause;
            //if (Obj_BtPlay != null) CUIEventTriggerListener.Get(Obj_BtPlay).onClick = _onclickPlay;


            float fixedAspectRatio = FixedWidth / FixedHeight;
            float screenWidth = (Application.isEditor && Camera.main != null) ? Camera.main.pixelWidth : Screen.width;
            float screenHeight = (Application.isEditor && Camera.main != null) ? Camera.main.pixelHeight : Screen.height;
            float aspectRatio = screenWidth / screenHeight;
            //if (aspectRatio > fixedAspectRatio)
            //    Img_Screen.rectTransform.sizeDelta = new Vector2(screenHeight * fixedAspectRatio, screenHeight) / CModuleManagerCenter.Instance.ScreenScale;
            //else
            //    Img_Screen.rectTransform.sizeDelta = new Vector2(screenWidth, screenWidth / fixedAspectRatio) / CModuleManagerCenter.Instance.ScreenScale;
            //Img_Screen.rectTransform.anchoredPosition = -CModuleManagerCenter.Instance.RootAnchorPos;

            m_VideoPlayer.playOnAwake = false;
            m_AudioSource.playOnAwake = false;

            m_VideoPlayer.source = VideoSource.VideoClip;
            m_VideoPlayer.audioOutputMode = VideoAudioOutputMode.AudioSource;
            m_VideoPlayer.EnableAudioTrack(0, true);
            m_VideoPlayer.SetTargetAudioSource(0, m_AudioSource);

            //添加prepareCompleted事件监听，videoPlayer.texture在 videoPlayer.Prepare()事件完成后才不为空  
            m_VideoPlayer.loopPointReached += _complete;

            m_bModuleReady = true;
            _initContent();

        }

        void OnEnable()
        {
            if (m_bModuleReady)
                _initContent();
        }

        private void _initContent()
        {
            //if (!CLocalInfo.Instance.IsPvOpen)
            //{
            //    _close();
            //    return;
            //}
            //CCommonUtility.SetActive(m_Buttons, false);
            //CCommonUtility.SetActive(Obj_BtPause, true);
            //CCommonUtility.SetActive(Obj_BtPlay, false);
            //Img_Screen.color = Color.clear;
            //if (CAudioManager.Instance.BgAudioClip != null && CAudioManager.Instance.BgAudioClip.IsPlaying)
            //{
            //    LastBgmId = CAudioManager.Instance.BgAudioClip.AudioTableInfo.ID;
            //    CAudioManager.Instance.BgAudioClip.Recycle();
            //}

            StartCoroutine(play());
        }

        private IEnumerator play()
        {
            //CPromotionVideoTableInfo tableInfo = CPromotionVideoTable.Instance.GetPromotionVideoTableInfo(CPVModule.Instance.ID);
            //if (tableInfo == null)
            //{
            //    GCore.Foundation.Log.LogErrorMsg("PromotionVideo表中不存在ID:{0}", CPVModule.Instance.ID);
            //    _close();
            //    yield break;
            //}
            //videoToPlay = CResourceManager.Instance.Load<VideoClip>(tableInfo.VideoClip);
            videoToPlay = Resources.Load<VideoClip>("Wildlife");
            if (videoToPlay == null)
            {
                _close();
                yield break;
            }

            m_VideoPlayer.clip = videoToPlay;
            m_VideoPlayer.Prepare();

            while (!m_VideoPlayer.isPrepared)
                yield return null;

            yield return null;

            m_AudioSource.volume = 1;
            m_VideoPlayer.Play();
            m_AudioSource.Play();

            Img_Screen.texture = m_VideoPlayer.texture;
            Img_Screen.color = Color.white;
        }

        private void _complete(VideoPlayer videoplayer)
        {
            _close();
        }

        //private void _onclickVideo(GameObject go)
        //{
        //    CCommonUtility.SetActive(m_Buttons, !m_Buttons.activeSelf);
        //}

        private void _onclickPass(GameObject go)
        {
            m_VideoPlayer.Stop();
            _close();
        }

        private void _onclickPause(GameObject go)
        {
            //CCommonUtility.SetActive(Obj_BtPause, false);
            //CCommonUtility.SetActive(Obj_BtPlay, true);
            m_VideoPlayer.Pause();
        }

        private void _onclickPlay(GameObject go)
        {
            //CCommonUtility.SetActive(Obj_BtPause, true);
            //CCommonUtility.SetActive(Obj_BtPlay, false);
            m_VideoPlayer.Play();
        }

        private void _close()
        {
            //if (CPVModule.Instance.CallBack != null)
            //    CPVModule.Instance.CallBack();
            //CPVModule.Instance.Close();
            //if (LastBgmId > 0)
            //{
            //    CNutEventPlayMusic Music = CNutShellCore.Instance.GameFactory.GetObject<CNutEventPlayMusic>();
            //    Music.IsBackMusic = true;
            //    Music.MusicTableId = LastBgmId;
            //    CNutShellCore.Instance.GameEventSystem.IssueEvent(Music);
            //    LastBgmId = 0;
            //}
            //else
            //    CAudioManager.Instance.ReturnToLobbyBGM();
        }


        //protected override void AddStateControl()
        //{
        //    base.AddStateControl();
        //    m_ModuleInstance.AddStateControl(0, (int)m_ModuleType, gameObject);
        //}

    }
}